﻿#include "TroposphereCorrection.h"
#include <cmath>
#include <iostream>

TroposphereCorrection::TroposphereCorrection() {
    // 初始化Saastamoinen模型参数
    pressure = 1013.25; 
    temperature = 293.15;
    humidity = 0.5; 
}

TroposphereCorrection::~TroposphereCorrection() {
}

double TroposphereCorrection::calculateTroposphereDelaySaastamoinen(const BaseT& time, const BLH& receiverPos, const XYZ& satellitePos) {
    // 将接收机的BLH坐标转换为XYZ坐标
    XYZ receiverXYZ = CoordSys().BLH2XYZ(receiverPos);
    // 调用输入接收机XYZ坐标的重载函数进行计算
    return calculateTroposphereDelaySaastamoinen(time, receiverXYZ, satellitePos);
}


double TroposphereCorrection::calculateTroposphereDelaySaastamoinen(const BaseT& time, const XYZ& receiverPos, const XYZ& satellitePos) {
    // 初始化对流层延迟误差值
    double delay = 0.0;

    // 计算卫星和接收机之间的差值向量
    XYZ diff = { satellitePos.x - receiverPos.x, satellitePos.y - receiverPos.y, satellitePos.z - receiverPos.z };
    // 计算卫星和接收机之间的距离
    double distance = sqrt(diff.x * diff.x + diff.y * diff.y + diff.z * diff.z);
    // 计算卫星的仰角
    double elevation = asin(diff.z / distance);

    // 根据Saastamoinen模型公式计算对流层延迟误差
    delay = 0.0023 * pressure / (0.0065 * temperature + 0.00065 * humidity);
    // 考虑卫星仰角的影响
    delay /= sin(elevation);

    return delay;
}
 
void TroposphereCorrection::test() {
    // 定义测试时间
    BaseT time = { 1491534395, 0.0 }; 
    // 定义测试接收机位置
    XYZ receiverPos = { -2091342.6543, 4800418.4462, 3629758.3665 }; 
    // 定义测试卫星位置
    XYZ satellitePos = { -32348377, 27042006, 509548 };

    // 计算对流层延迟误差
    double delay = calculateTroposphereDelaySaastamoinen(time, receiverPos, satellitePos);
    // 输出对流层延迟误差改正值
    std::cout << "对流层延迟误差改正值:" << delay << " 米" << std::endl;
}
